import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 路由懒加载
const Layout = () => import('layout/Layout')
const Login = () => import('views/login/Login')
const Home = () => import('views/home/Home')
const Ocean = () => import('views/ocean/Ocean')
const OceanList = () => import('views/ocean/childComps/List')
const OceanAdd = () => import('views/ocean/childComps/Add')
const OceanUpdate = () => import('views/ocean/childComps/Update')
const Animal = () => import('views/animal/Animal')
const AnimalList = () => import('views/animal/childComps/List')
const AnimalAdd = () => import('views/animal/childComps/Add')
const AnimalUpdate = () => import('views/animal/childComps/Update')
const Article = () => import('views/article/Article')
const ArticleList = () => import('views/article/childComps/List')
const ArticleAdd = () => import('views/article/childComps/Add')
const ArticleUpdate = () => import('views/article/childComps/Update')
const Type = () => import('views/type/Type')
const AnimalType = () => import('views/type/childComps/AnimalType')
const ArticleType = () => import('views/type/childComps/ArticleType')
const Reason = () => import('views/reason/Reason')
const AuditingReason = () => import('views/reason/childComps/AuditingReason')
const ReportReason = () => import('views/reason/childComps/ReportReason')
const ArticleReason = () => import('views/reason/childComps/ArticleReason')
const CommentReason = () => import('views/reason/childComps/CommentReason')
const Audit = () => import('views/audit/Audit')
const ArticleAudit = () => import('views/audit/childComps/ArticleAudit')
const ReportAudit = () => import('views/audit/childComps/ReportAudit')
const ArticleReport = () => import('views/audit/childComps/ArticleReport')
const CommentReport = () => import('views/audit/childComps/CommentReport')
const Activity = () => import('views/activity/Activity')
const ActivityList = () => import('views/activity/childComps/List')
const ActivityAdd = () => import('views/activity/childComps/Add')
const ActivityUpdate = () => import('views/activity/childComps/Update')
const Users = () => import('views/users/Users')
const UsersList = () => import('views/users/childComps/List')
const UsersThaw = () => import('views/users/childComps/Thaw')
const Admin = () => import('views/admin/Admin')
const UpdatePwd = () => import('views/admin/childComps/UpdatePwd')
const UpdateAvatar = () => import('views/admin/childComps/UpdateAvatar')

const routes = [
  {
    path: '/login',
    component: Login,
    name: 'Login'
  },
  // 使用路由前置守卫，有token时才可以访问，没有跳转登录
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        component: Home
      },
      // 海洋
      {
        path: 'ocean',
        component: Ocean,
        redirect: '/ocean/list',
        children: [
          {
            path: 'list',
            component: OceanList
          },
          {
            path: 'add',
            component: OceanAdd
          },
          {
            path: 'update',
            component: OceanUpdate
          }
        ]
      },
      // 生物
      {
        path: 'animal',
        component: Animal,
        redirect: '/animal/list',
        children: [
          {
            path: 'list',
            component: AnimalList
          },
          {
            path: 'add',
            component: AnimalAdd
          },
          {
            path: 'update',
            component: AnimalUpdate
          }
        ]
      },
      // 文章
      {
        path: 'article',
        component: Article,
        redirect: '/article/list',
        children: [
          {
            path: 'list',
            component: ArticleList
          },
          {
            path: 'add',
            component: ArticleAdd
          },
          {
            path: 'update',
            component: ArticleUpdate
          }
        ]
      },
      // 分类
      {
        path: 'type',
        component: Type,
        redirect: '/type/animal',
        children: [
          {
            path: 'animal',
            component: AnimalType
          },
          {
            path: 'article',
            component: ArticleType
          }
        ]
      },
      // 原因
      {
        path: 'reason',
        component: Reason,
        redirect: '/reason/auditing',
        children: [
          {
            path: 'auditing',
            component: AuditingReason
          },
          {
            path: 'report',
            component: ReportReason,
            redirect: '/report/article',
            children: [
              {
                path: 'article',
                component: ArticleReason
              },
              {
                path: 'comment',
                component: CommentReason
              }
            ]
          }
        ]
      },
      // 审核
      {
        path: 'audit',
        component: Audit,
        redirect: '/audit/article',
        children: [
          {
            path: 'article',
            component: ArticleAudit
          },
          {
            path: 'report',
            component: ReportAudit,
            redirect: '/report/article',
            children: [
              {
                path: 'article',
                component: ArticleReport
              },
              {
                path: 'comment',
                component: CommentReport
              }
            ]
          }
        ]
      },
      // 志愿活动
      {
        path: 'activity',
        component: Activity,
        redirect: '/activity/list',
        children: [
          {
            path: 'list',
            component: ActivityList
          },
          {
            path: 'add',
            component: ActivityAdd
          },
          {
            path: 'update',
            component: ActivityUpdate
          }
        ]
      },
      // 用户
      {
        path: 'users',
        component: Users,
        redirect: '/users/list',
        children: [
          {
            path: 'list',
            component: UsersList
          },
          {
            path: 'thaw',
            component: UsersThaw
          }
        ]
      },
      // 管理员
      {
        path: 'admin',
        component: Admin,
        redirect: '/admin/updatePwd',
        children: [
          {
            path: 'updatePwd',
            component: UpdatePwd
          },
          {
            path: 'updateAvatar',
            component: UpdateAvatar
          }
        ]
      }
    ]
  },
]

const router = new VueRouter({
  routes,
  mode: 'history'
})

// 全局前置守卫 判断是否为登录状态 没有登录返回登录页面 登录了可以访问除登录外的所有界面
router.beforeEach((to, from, next) => {
  let admin = sessionStorage.getItem('admin')
  if (to.name !== 'Login' && !admin) next({ name: 'Login' })
  else next()
})

// 防止重复点击当前页面报错
const originalPush = VueRouter.prototype.push
   VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

export default router